package com.linwen.excise.leetcode;

/**
 * @ClassName _198打家劫舍
 * @Description TODO
 * @Author zero
 * @DATE 2024/4/7 2:50 AM
 * @Version 1.0
 */
public class _198打家劫舍 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,1};
        System.out.println(rob(arr));
    }

    public static int rob(int[] nums) {
        if(nums==null||nums.length==0){
            return 0;
        }
        int[] dp = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            if(i==0){
                dp[0] = nums[0];
            }
            if (i==1){
                dp[1] = Math.max(dp[0],nums[1] );
            }
            if(i>1){
                dp[i] = Math.max(dp[i-1],dp[i-2]+nums[i]);
            }
        }
        return dp[nums.length-1];
    }
}
